Template for Bivariate Risk Maps

Column

Bivariate country map

Column

Scatter plot

Legend

---
title: "Estimating the burden of COVID-19 in African countries"
output: 
  flexdashboard::flex_dashboard:
    vertical_layout: scroll
    source_code: embed
    theme: bootstrap
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(message = FALSE, warning = FALSE)

# Libraries
library(tidyverse)
library(glue)
library(sf)
library(cowplot)
library(stringr)
library(patchwork)
library(scales)
library(plotly)
library(leaflet)
library(htmltools)
library(here)

source(here("R/_functions/map_bivar.R"))
```

Template for Bivariate Risk Maps
======================================================================

Options {.sidebar}
-------------------------------------

see [this](https://mkiang.shinyapps.io/county_risks/) for inspiration.

Options should include:

- X Variable: dropdown list from indicators
- Transform X: log or sqrt 

- Y Variable: dropdown list from indicators
- Transform Y: log or sqrt

- Color scale: Categorical or Continuous

Eventually: figure out different scales (only for subset of indicators that we have data for at finer scales), 
i.e. admin 1, admin 2, city, grid cell)? If worth it?

```{r}
# Load in country dataset
countries <- st_read(here("data/processed/shapefiles/country.shp"), 
                     quiet = TRUE)
countries$loc_id <- countries$iso # for matching to indicator data

indicators <- read_csv(here("data/processed/SSA.health.indicators.csv"))

indicators %>%
  group_by(alias) %>%
  summarize(label = gsub("popn", "people", indicator_label_standard[1])) -> indicator_labs

indicators %>% 
  pivot_wider(id_cols = c("COUNTRY_CODE", "COUNTRY_NAME"), names_from = alias, 
              values_from = value) -> indicators_wide

test <- map_bivariate(x = indicators_wide$`1_1_physicians_p100k`, 
                      y = indicators_wide$`2_6_p_handwashing_at_hh_urban`, 
                      lab_x = indicator_labs$label[indicator_labs$alias == "1_1_physicians_p100k"], 
                      lab_y = indicator_labs$label[indicator_labs$alias == "2_6_p_handwashing_at_hh_urban"],
                      loc_ids = indicators_wide$COUNTRY_CODE, 
                      loc_labs = indicators_wide$COUNTRY_NAME,
                      sf_obj = countries, continuous = TRUE, trans_x = "log", trans_y = "log", 
                      probs_x = c(0.25, 0.75), probs_y = c(0.25, 0.75), static = TRUE, 
                      rev_x = TRUE, rev_y = TRUE)

ggsave("figs/main/bivariate_example.jpeg", test, height = 8, width = 8)

# For shiny app
test <- map_bivariate(x = indicators_wide$`1_1_physicians_p100k`, 
                      y = indicators_wide$`2_6_p_handwashing_at_hh_urban`, 
                      lab_x = indicator_labs$label[indicator_labs$alias == "1_1_physicians_p100k"], 
                      lab_y = indicator_labs$label[indicator_labs$alias == "2_6_p_handwashing_at_hh_urban"],
                      loc_ids = indicators_wide$COUNTRY_CODE, 
                      loc_labs = indicators_wide$COUNTRY_NAME,
                      sf_obj = countries, continuous = TRUE, trans_x = "log", trans_y = "log", 
                      probs_x = c(0.25, 0.75), probs_y = c(0.25, 0.75), static = FALSE, 
                      rev_x = TRUE, rev_y = TRUE)

```

Column {data-width=600}
-------------------------------------
### Bivariate country map 

```{r}
test$map
```

Column {data-width=400}
-------------------------------------

### Scatter plot 

```{r}
test$scatter
```

### Legend 

```{r}
test$legend
```